// 把读取到的蓝牙设备解析为可读的数据
import moment from 'moment'
var advData

// 转二进制
const toBinary = num => {
	const bin = parseInt(num).toString(2)
	const place = 8 - bin.length
	const intact = '0'.repeat(place) + bin
	const arr = intact.split('')
	return arr.map(Number)
}

// mainState
const mainState = (data) => {
	let arr = [
		toBinary(advData[2]),
		toBinary(advData[3]),
		toBinary(advData[4]),
		toBinary(advData[5])
	]
	return arr.reverse()
}

// 设置mid
const setMid = () => {
	if ((advData[5] & 0x10) == 0x10) {
		let num = (advData[8] << 24) | (advData[9] << 16) | (advData[10] << 8) | (advData[11])
		return ('00000000' + num).slice(-9)
	} else {
		return ''
	}
}


// 设备记录状态
// (advData[5] & 0x02) == 0x02 记录中
// ((advData[3] & 0x02) == 0x02) 已停止
const setConfigState = () => {
	const state = (advData[5] & 0x02) == 0x02 ? 1 : (((advData[3] & 0x02) == 0x02) ? 0 : 3)
	return state
}

// 未配置过的
const setTagConfiged = () => {
	const state = (advData[5] & 0x08) == 0x08
	return state
}

// 设备读取状态
const setReadStatus = () => {
	return false
}

// 超标状态
const setExcessive = () => {
	const value = ((advData[4] & 0x20) == 0x20)
	return value
}

// 设置时间
const setNowtempTime = (view) => {
	let timezone = advData[17] + 8
	const timese = view.getUint32(13) * 1000 - timezone * 60 * 60 * 1000
	let d = new Date()
	let d1 = Date.UTC(2000, 0, 1, 0, 0, 0)
	if (timezone - 12 > 0) {
		timezone = (timezone - 12) * (-1)
	}
	d.setTime(timese + d1)
	return moment(d).format('YYYY-MM-DD HH:mm:ss')
}

// 是否设置查询码
const isSetCheckId = () => {
	return (advData[3] & 0x80) == 0x80
}
// 查询码解析
const parseCheckId = () => {
	let num = ''
	for(let i = 0; i < 6; i++) {
		num +=  String.fromCharCode(advData[19 + i])
	}
	return num
}
// 当前温度
const setNowTemplate = (view) => {
	const template = view.getInt16(6) // 温度
	return (template / 100).toFixed(1)
}
// 电量
const setBatteryVal = () => {
	let val = advData[12]
	return parseInt(val.toString(16), 16)
}
// 设备状态
const devState = () => {
	const state = mainState()
	let type = ''
	if(!state.length) {
		return false
	}
	if(!state[0][6]) {
		if(state[2][6]) {
			type = 'stop'
		} else {
			type = 'setout'
		}
	} else {
		if(!state[2][6]) {
			type = 'rec'
		}
	}

	return type
}

export default function(data, type) {
	let {...params} = data
	advData = new Uint8Array(params.advertisData)
	// 转换
	const buffer = advData.buffer
	const view = new DataView(buffer)
	const state = mainState()
	// (advData[5] & 0x08) == 0x08 未配置
	params.mid = setMid() || 'ID为空'
	params.nowtempTime = setNowtempTime(view)
	params.configed = setConfigState() // 记录状态
	if(type == 'get') {
		params.readStatus = setReadStatus() // 读取状态
	} else {
		params.state2 = true
		params.tagConfiged = setTagConfiged() // 未配置的
	}
	params.excessiveValue = setExcessive() // 超标
	params.customId = isSetCheckId()
	params.nowTemplate = setNowTemplate(view)
	params.batteryVal = setBatteryVal()
	params.state = devState() // 状态 （stop: 停止，setout：待配置，rec: 记录中）
	if(params.customId) {
		params.customCheckID = parseCheckId()
	}
	return params
}